import {User} from "../../../../entity/User";
/**
 * Created by enixjin on 1/10/18.
 */
import {UserService} from "../../../../service/userService";
import {AbstractDataAction} from "../../AbstractDataAction";
import {AbstractUserAction} from "../AbstractUserAction";
import {AbstractUserRESTAction} from "../AbstractUserRESTAction";
import {UserDeleteCompleteAction} from "../data/UserDeleteCompleteAction";
import {UserDeleteRESTAction} from "../rest/UserDeleteRESTAction";


export class UserDeleteAction extends AbstractUserAction {

    constructor(public userService: UserService, public payload: User) {
        super();
    }

    getRestAction(): AbstractUserRESTAction {
        return new UserDeleteRESTAction(this.payload, this.userService, this.getLocalKey());
    }

    getDataAction(local): AbstractDataAction {
        return new UserDeleteCompleteAction(this.loadFromLocal(local));
    }

    updateLocal(local): void {
        let result = local.get(this.getLocalKey()).map(u => u === this.payload ? Object.assign(u, {deleted: true}) : u);
        console.log(result);
        local.set(this.getLocalKey(), result);
    }

    loadFromLocal(local): any {
        // return local.get(this.getLocalKey()).find(u => u.id === this.payload.id);
    }
}
